﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameEngine.EditorObject;

using AimGameLibrary;

namespace AimGameEngine.NodeEditorObject
{
    public class NodeController : NodeBaseControl
    {
        public Dictionary<String, NodeObject> DictionaryObjectUpdatePressed;
        public Dictionary<String, NodeObject> DictionaryObjectUpdateRelease;
        public Dictionary<String, NodeObject> DictionaryObjectUpdateIsup;
        public Dictionary<String, NodeObject> DictionaryObjectUpdateIsdown;

        public NodeController(SpriteBatch inputspritebatch, Camera inputcamera, NodeData inputData
            , List<IDraw> MyListDraw, List<IUpdateMainTime> MyListUpdateTime, Dictionary<String, NodeObject> MyListObject, Dictionary<String, NodeObject> MyListType)
            : base(inputspritebatch, inputcamera, inputData, MyListDraw,MyListUpdateTime, MyListObject, MyListType)
	    {
            DictionaryObjectUpdatePressed = new Dictionary<string, NodeObject>();
            DictionaryObjectUpdateRelease = new Dictionary<string, NodeObject>();
            DictionaryObjectUpdateIsup = new Dictionary<string, NodeObject>();
            DictionaryObjectUpdateIsdown = new Dictionary<string, NodeObject>();

            Connecter outputBoolean = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.KeyController, this);
            ListConnectpoint.Add(outputBoolean);

            Connecter outputUpdate = new Connecter(inputspritebatch, inputcamera, Syco.Output, NodeTypeValue.Update, this);
            ListConnectpoint.Add(outputUpdate);

            Connecter inputPressed = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Update, this);
            ListConnectpoint.Add(inputPressed);

            Connecter inputRelease = new Connecter(inputspritebatch, inputcamera, Syco.Input, NodeTypeValue.Update, this);
            ListConnectpoint.Add(inputRelease);
	    }


        public override void SetupInput(Connecter Thisconnect, Connecter connectOutput)
        {
            //base.SetupInput(Thisconnect, connectOutput);
            if (InputMode == NodeInputMode.Pressed_Release)
            {
                switch (ListConnectpointInput.IndexOf(Thisconnect))
                {
                    case 0: DictionaryObjectUpdatePressed.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                    case 1: DictionaryObjectUpdateRelease.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                }
                //MyData.Data = NameBody1 + "|" + NameBody2;
            }
            else if (InputMode == NodeInputMode.Isdown_Isup)
            {
                switch (ListConnectpointInput.IndexOf(Thisconnect))
                {
                    case 0: DictionaryObjectUpdateIsdown.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                    case 1: DictionaryObjectUpdateIsup.Add(connectOutput.MyNode.MyData.MyName, connectOutput.MyNode); break;
                }
            }
        }

        public override void ClearInput(Connecter ThisInputConnect, Connecter ThisConnectClear, string NameThisClear)
        {
            //base.ClearInput(ThisConnectClear, NameThisClear);
            if (InputMode == NodeInputMode.Pressed_Release)
            {
                switch (ListConnectpointInput.IndexOf(ThisInputConnect))
                {
                    case 0: DictionaryObjectUpdatePressed.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
                    case 1: DictionaryObjectUpdateRelease.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
                }
                //MyData.Data = NameBody1 + "|" + NameBody2;
            }
            else if (InputMode == NodeInputMode.Isdown_Isup)
            {
                switch (ListConnectpointInput.IndexOf(ThisInputConnect))
                {
                    case 0: DictionaryObjectUpdateIsdown.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
                    case 1: DictionaryObjectUpdateIsup.Remove(ThisConnectClear.MyNode.MyData.MyName); break;
                }
            }
        }

        public override void SetupProprety()
        {
            base.SetupProprety();

            if (InputMode == NodeInputMode.Isdown_Isup)
            {
                Connecter IskeyUp = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Update, this);
                ListConnectpoint.Add(IskeyUp);

                Connecter IskeyDown = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Update, this);
                ListConnectpoint.Add(IskeyDown);
            }
            else if (InputMode == NodeInputMode.Pressed_Release)
            {
                Connecter inputPressed = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Update, this);
                ListConnectpoint.Add(inputPressed);

                Connecter inputRelease = new Connecter(this.spritebatch, this.camera, Syco.Input, NodeTypeValue.Update, this);
                ListConnectpoint.Add(inputRelease);
            }

            this.AfterSetupProprety();
        }

        protected override Dictionary<string, NodeObject> GetDictionaryNode(Dictionary<string, NodeObject> TempNode)
        {
            foreach (String keyIsDown in DictionaryObjectUpdateIsdown.Keys)
                if (MyDictionaryObject.ContainsKey(keyIsDown) && !TempNode.ContainsKey(keyIsDown))
                    TempNode.Add(keyIsDown, MyDictionaryObject[keyIsDown]);

            foreach (String keyIsUp in DictionaryObjectUpdateIsup.Keys)
                if (MyDictionaryObject.ContainsKey(keyIsUp) && !TempNode.ContainsKey(keyIsUp))
                    TempNode.Add(keyIsUp, MyDictionaryObject[keyIsUp]);

            foreach (String keyPressed in DictionaryObjectUpdatePressed.Keys)
                if (MyDictionaryObject.ContainsKey(keyPressed) && !TempNode.ContainsKey(keyPressed))
                    TempNode.Add(keyPressed, MyDictionaryObject[keyPressed]);

            foreach (String keyRelease in DictionaryObjectUpdateRelease.Keys)
                if (MyDictionaryObject.ContainsKey(keyRelease) && !TempNode.ContainsKey(keyRelease))
                    TempNode.Add(keyRelease, MyDictionaryObject[keyRelease]);

            return base.GetDictionaryNode(TempNode);
        }

        protected void SaveDataDictionary(System.Collections.ArrayList Thislist)
        {
            if (InputMode == NodeInputMode.Isdown_Isup)
            {
                Thislist.Add(DictionaryObjectUpdateIsdown.Count);
                if (DictionaryObjectUpdateIsdown.Count > 0)
                    Thislist.Add(CreateData(DictionaryObjectUpdateIsdown));
                else Thislist.Add("NULL");

                Thislist.Add(DictionaryObjectUpdateIsup.Count);
                if (DictionaryObjectUpdateIsup.Count > 0)
                    Thislist.Add(CreateData(DictionaryObjectUpdateIsup));
                else Thislist.Add("NULL");
            }
            else if (InputMode == NodeInputMode.Pressed_Release)
            {
                Thislist.Add(DictionaryObjectUpdatePressed.Count);
                if (DictionaryObjectUpdatePressed.Count > 0)
                    Thislist.Add(CreateData(DictionaryObjectUpdatePressed));
                else Thislist.Add("NULL");

                Thislist.Add(DictionaryObjectUpdateRelease.Count);
                if (DictionaryObjectUpdateRelease.Count > 0)
                    Thislist.Add(CreateData(DictionaryObjectUpdateRelease));
                else Thislist.Add("NULL");
            }
        }
    }
}
